package com.cdc.common;

import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

public class CommonTool {
	private String getInitParam(ServletConfig servletConfig){
		StringBuilder sb=new StringBuilder();
		String admin=servletConfig.getInitParameter("admin");
		String email=servletConfig.getInitParameter("email");
		sb.append("admin=");
		sb.append(admin);
		sb.append("</br>");
		sb.append("email=");
		sb.append(email);
		sb.append("</br>");
		return sb.toString();
	}
	
	private String getRequestParame(ServletRequest request){
		StringBuilder sb=new StringBuilder();
		sb.append("getCharacterEncoding()=");
		sb.append(request.getCharacterEncoding());
		sb.append("</br>");
		sb.append("getContentType()=");
		sb.append(request.getContentType());
		sb.append("</br>");
		sb.append("getContentLength()=");
		sb.append(request.getContentLength());
		sb.append("</br>");
		sb.append("getContentLengthLong()=");
		sb.append(request.getContentLengthLong());
		sb.append("</br>");
		sb.append("getLocalAddr()=");
		sb.append(request.getLocalAddr());
		sb.append("</br>");
		sb.append("getLocalName()=");
		sb.append(request.getLocalName());
		sb.append("</br>");
		sb.append("getLocalPort()=");
		sb.append(request.getLocalPort());
		sb.append("</br>");
		sb.append("getProtocol()=");
		sb.append(request.getProtocol());
		sb.append("</br>");
		sb.append("getRemoteAddr()=");
		sb.append(request.getRemoteAddr());
		sb.append("</br>");
		sb.append("getRemoteHost()=");
		sb.append(request.getRemoteHost());
		sb.append("</br>");
		sb.append("getRemotePort()=");
		sb.append(request.getRemotePort());
		sb.append("</br>");
		sb.append("getScheme()=");
		sb.append(request.getScheme());
		sb.append("</br>");
		sb.append("getServerName()=");
		sb.append(request.getServerName());
		sb.append("</br>");
		return sb.toString();
	}
	
	public String getHtml(ServletRequest request,ServletConfig servletConfig){
		StringBuilder sb=new StringBuilder();
		sb.append("<html><head></head>");
		sb.append("<body>");
		sb.append(setHtmlH1(servletConfig));
		sb.append(getHeader(request));
		String pHtml=getPHtml(request,servletConfig);
		if(pHtml!=null && pHtml.length()>0){
			sb.append(pHtml);
		}
		sb.append("<body></html>");
		return sb.toString();
	}
	
	private String getPHtml(ServletRequest request,ServletConfig servletConfig){
		StringBuilder sb=new StringBuilder();
		sb.append("<p>");
		if(request!=null){
			sb.append(getRequestParame(request));
		}
		if(servletConfig!=null){
			sb.append(getInitParam(servletConfig));
		}
		sb.append("</p>");
		return sb.toString();
	}
	
	private String setHtmlH1(ServletConfig servletConfig){
		StringBuilder sb=new StringBuilder();
		sb.append("<h1>");
		sb.append(servletConfig.getServletName());
		sb.append("</h1>");
		return sb.toString();
	}
	
	private String getHeader(ServletRequest request){
		StringBuilder sb=new StringBuilder();
		sb.append("<p>");
		Enumeration<?> enums = request.getAttributeNames();
        while (enums.hasMoreElements()) {
            String headerName = (String) enums.nextElement();
            String headerValue = (String) request.getAttribute(headerName);
            sb.append("<b>" + headerName + "</b>: ");
            sb.append(headerValue + "<br/>");
        }
		sb.append("</p>");
		return sb.toString();
	}
	
	public String getHeader(HttpServletRequest request){
		StringBuilder sb=new StringBuilder();
		sb.append("<p>");
		Enumeration<?> enums = request.getHeaderNames();
        while (enums.hasMoreElements()) {
            String headerName = (String) enums.nextElement();
            String headerValue = (String) request.getHeader(headerName);
            sb.append("<b>" + headerName + "</b>: ");
            sb.append(headerValue + "<br/>");
        }
		sb.append("</p>");
		return sb.toString();
	}
	
	


}
